package com.lanou3g.baidumusic.main.musicfragment.songlistfragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.baidumusic.R;
import com.lanou3g.baidumusic.main.tools.RecyclerViewOnClickListener;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/17.
 */

public class SongListAdapter extends RecyclerView.Adapter<SongListAdapter.SongListHolder>{

    private SongListBean songListBean ;
    private Context context ;

    private RecyclerViewOnClickListener listener ;
    private SongListHolder holder;

    public void  setSongListBean (SongListBean songListBean){
        this.songListBean = songListBean ;
        notifyDataSetChanged();
    }

    public SongListAdapter(Context context) {
        this.context = context;
    }

    @Override
    public SongListHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_fragment_song_list , parent , false);

        holder = new SongListHolder(view);

        return holder;

    }

    @Override
    public void onBindViewHolder(SongListHolder holder, final int position) {


        holder.nameTv.setText(songListBean.getDiyInfo().get(position).getTitle());
        holder.fromTv.setText("By: "+songListBean.getDiyInfo().get(position).getUsername());

        Picasso.with(context).load(songListBean.getDiyInfo().get(position).getList_pic_huge()).into(holder.picIv);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                listener.onItemClick(position);

            }
        });


    }

    @Override
    public int getItemCount() {

        if (null == songListBean){
            return 0 ;
        }
        if (null == songListBean.getDiyInfo()){
            return 0 ;
        }

        return songListBean.getDiyInfo() != null && songListBean.getDiyInfo().size() > 0 ?
                songListBean.getDiyInfo().size() : 0;

    }



    public class SongListHolder extends RecyclerView.ViewHolder{

        ImageView picIv ;
        TextView nameTv , fromTv ;

        public SongListHolder(View itemView) {
            super(itemView);
            picIv = (ImageView) itemView.findViewById(R.id.item_song_list_iv);
            nameTv = (TextView) itemView.findViewById(R.id.item_song_list_name_tv);
            fromTv = (TextView) itemView.findViewById(R.id.item_song_list_from_tv);

        }
    }

    public void setRecyclerListener(RecyclerViewOnClickListener listener){
        this.listener = listener ;
    }



}
